package com.roads.workflow.controller;

import java.util.ArrayList;
import java.util.List;
import com.roads.common.core.domain.entity.SysUser;
import com.roads.system.service.ISysUserService;
import com.roads.workflow.domain.WorkflowMeterageDefinition;
import com.roads.workflow.domain.WorkflowMeterageNodeUser;
import com.roads.workflow.service.IWorkflowMeterageDefinitionService;
import com.roads.workflow.service.IWorkflowMeterageNodeUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.enums.BusinessType;
import com.roads.workflow.domain.WorkflowMeterageNode;
import com.roads.workflow.service.IWorkflowMeterageNodeService;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.common.core.page.TableDataInfo;

/**
 * 流程管理-计量流程节点Controller
 * 
 * @author nhp
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/workflow/node")
public class WorkflowMeterageNodeController extends BaseController {

    @Autowired
    private IWorkflowMeterageNodeService service;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IWorkflowMeterageNodeUserService workflowMeterageNodeUserService;

    @Autowired
    private IWorkflowMeterageDefinitionService workflowMeterageDefinitionService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:meterage:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkflowMeterageNode model) {
        startPage();
        List<WorkflowMeterageNode> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('workflow:meterage:export')")
    @Log(title = "流程管理-计量流程节点", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WorkflowMeterageNode model) {
        List<WorkflowMeterageNode> list = service.selectModelList(model);
        ExcelUtils<WorkflowMeterageNode> util = new ExcelUtils<WorkflowMeterageNode>(WorkflowMeterageNode.class);
        return util.exportExcel(list, "流程管理-计量流程节点数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('workflow:meterage:query')")
    @GetMapping(value = "/{workflowNodeUuid}")
    public AjaxResult getInfo(@PathVariable("workflowNodeUuid") String workflowNodeUuid) {
        return AjaxResult.success(service.getModelById(workflowNodeUuid));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('workflow:meterage:add')")
    @Log(title = "计量流程节点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkflowMeterageNode model) {
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('workflow:meterage:edit')")
    @Log(title = "计量流程节点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkflowMeterageNode model) {
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('workflow:meterage:remove')")
    @Log(title = "计量流程节点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        //////// 流程节点删除限制
        return toAjax(service.deleteByIds(ids));
    }

    /**
     * 获取流程节点用户
     *
     * @param workflowNodeUuid 机构内部编码
     * @return 结果
     */
    @PostMapping("/getWorkflowUserList")
    public AjaxResult getWorkflowUserList(String workflowNodeUuid) {
        AjaxResult ajax = new AjaxResult();
        WorkflowMeterageNode workflowMeterageNode = service.getModelById(workflowNodeUuid);
        WorkflowMeterageDefinition workflowMeterageDefinition = workflowMeterageDefinitionService.getModelById(workflowMeterageNode.getWorkflowUuid());
        List<SysUser> users;
        if (workflowMeterageNode.getWorkflowNodeOrder().equals(1)) {
            users = sysUserService.transferWorkflowUserBeginNode(workflowMeterageDefinition.getWorkflowType(), workflowNodeUuid);
        } else {
            users = sysUserService.transferWorkflowUserOtherNode(workflowMeterageDefinition.getWorkflowType());
        }
        WorkflowMeterageNodeUser search = new WorkflowMeterageNodeUser();
        search.setWorkflowNodeUuid(workflowNodeUuid);
        List<WorkflowMeterageNodeUser> checkedUsers = workflowMeterageNodeUserService.selectModelList(search);
        List<String> userList = new ArrayList<>();
        if (checkedUsers.size() > 0) {
            for (WorkflowMeterageNodeUser user: checkedUsers) {
                userList.add(user.getUser());
            }
        }
        ajax.put("users", users);
        ajax.put("checkedUsers", userList);
        return ajax;
    }

    @PostMapping("/saveWorkflowNodeUser")
    @PreAuthorize("@ss.hasPermi('workflow:meterage:add')")
    @Log(title = "计量流程节点用户", businessType = BusinessType.INSERT)
    public AjaxResult saveWorkflowNodeUser(String workflowNodeUuid, String users) {
        return toAjax(workflowMeterageNodeUserService.saveWorkflowNodeUser(workflowNodeUuid, users));
    }

}
